package com.silence.gankio.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import androidx.navigation.Navigation
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.silence.gankio.mvp.control.SplashControl
import com.silence.gankio.mvp.presenter.SplashPresenterImpl
import com.silence.gankio.sp.SplashSP
import com.silence.gankio.util.ToastUtil
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.splash_fragment.*
import java.util.concurrent.TimeUnit

class SplashFragment : Fragment(), SplashControl.ISplashView {
    override fun applyDisposable(disposable: Disposable?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onNetFinish() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    companion object {
        fun newInstance() = SplashFragment()
    }

    private lateinit var mPresenter: SplashControl.ISplashPresenter

//    override fun getFragmentContentView() = R.layout.splash_fragment

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mPresenter = SplashPresenterImpl(this)

        mPresenter.loadImageUrl()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initCountdown(view)
        Glide.with(this)
            .load(SplashSP.INSTANCE.getImageUrl())
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(iv_splash)

        tv_countdown.setOnClickListener {
            Navigation.findNavController(view).popBackStack()
//            Navigation.findNavController(view).navigate(R.id.action_splashFragment_to_mainFragment, null, mNavOptions)

        }
    }

    private fun initCountdown(view: View) {
        Observable.intervalRange(1, 5, 0, 1, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Long> {
                override fun onComplete() {
                }

                override fun onSubscribe(d: Disposable) {
                    applyDisposable(d)
                }

                override fun onNext(t: Long) {
                    val currentCount = 5 - t
                    if (currentCount == 0L) {
                        Navigation.findNavController(view).popBackStack()
//                        Navigation.findNavController(view).navigate(R.id.action_splashFragment_to_mainFragment, null, mNavOptions)
                    } else {
                        tv_countdown.text = currentCount.toString()
                    }
                }

                override fun onError(e: Throwable) {
                }
            })
    }

    override fun onLoadImageUrlSuccess(url: String?) {
        ToastUtil.showToast(context, "已获取最新首页图片")
        Glide.with(this)
            .load(url)
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(iv_splash)
    }

    override fun onNetError(code: Int, msg: String?) {
//        super.onNetError(code, msg)
    }
}
